/**
 * 房間中撲克牌视图控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-8 13:32
 */
package com.addcn.texas.view
{

	import com.addcn.texas.view.components.Poker;
	import flash.display.*;
	import flash.events.Event;
	import flash.display.Stage;		
	import flash.events.*;
	import flash.utils.Timer;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;	
	import com.addcn.texas.utils.Chips;
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	
	import caurina.transitions.Tweener;
		
	public class PokerView extends View
	{				
		public static const POKER_RESULT : String = "pokerResult";
		public static const CLEAR_POKER : String = "clearPoker";
		public static const SHOW_POKER_RESULT : String = "showPokerResult"
		public static const ALLOT_CHIPS : String = "allotChips";
		
		private var _container : Object;
		private var _app : AppLibary 
		private var _poker_class :Class;		
		private var _card_class : Class;
		
		private var _public_poker : int;  //已經顯示的公共牌數				
		private var _table_cards : int;   //已发给玩家牌数
		private var _all_poker : Object;  //所有已顯示的撲克牌
		private var _user_poker :Object;  //玩家私有撲克牌,索引為座位号
		private var _table_poker : Object; //桌面上撲克,索引為座位号
		private var _cards_pos : Array;   //桌面上扑克牌位置
		private var _poker_pos : Array;   //玩家扑克牌位置
		private var _two_poker = { };  //已发给玩家的扑克牌数,索引为座位号
		private var _game_result : Object;  //游戏结果
		
		private var _my_sit : int;       //玩家自己的座位號
		private var _id_sit_list : Object; //玩家ID與座位號對應列表
		private var _my_poker : Array;   //玩家自己的撲克牌
		
		
		
		public function PokerView(r_target : Object) {
			_container = r_target;	
			_intialize();
		}			
		
		private function _intialize() : void
		{		
			//建立容器
			var _sprite = new Sprite;
			_container.addChild(_sprite);
			_container = _sprite;	
						
			//獲取撲克牌類
			_app = AppLibary.getInstance();	
			this._poker_class = _app.getClass("com.addcn.texas.view.components.Poker");
			this._card_class = _app.getClass("cardBack");
			
			this._public_poker = 0;
			this._table_cards = 0			
			this._all_poker = { };    
			this._user_poker = { };
			this._table_poker = { };
			this._two_poker = { };			
			//撲克坐標
			var _coord : Coordinate = Coordinate.getInstance();
			_cards_pos = _coord.CardCoord();
			_poker_pos = _coord.PokerCoord();
			
			
			
		}
						
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [										
								RoomUserModel.USER_STANDUP,
								GameInfoModel.GAME_START,
								GameInfoModel.DEAL_USER_POKER,
								GameInfoModel.DEAL_COMMUNITY_CARDS,		
								GameInfoModel.GAME_WIN_RESULT,
								GameInfoModel.GAME_OVER,	
								//ChipsView.MOVE_CHIPS_COMPLETE,
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {					
				case RoomUserModel.USER_STANDUP:
					_clearTableCards([r_data["sit"]]);
					_clearCards([r_data["sit"]]);
					this._two_poker = { };
					break;
				case GameInfoModel.GAME_START:					
					this._game_result = null;
					this._clearTableCards([1,2,3,4,5,6,7,8,9]);
					_clearPoker();
					
					break;
				case GameInfoModel.DEAL_USER_POKER:		
					this._clearPoker();
					this._clearTableCards([1,2,3,4,5,6,7,8,9]);
					var _sit : int = r_data["sit"];
					var _poker : Array = r_data["poker"];
					var _sit_list : Array = r_data["sitList"];
					this._user_poker[_sit] = _poker;					
					_setTableCards(_sit_list);				
					break;						
				case GameInfoModel.DEAL_COMMUNITY_CARDS: 			
					_setCommunityCards(r_data);
					break;						
				case GameInfoModel.GAME_WIN_RESULT:					
					_showWinUserPoker(r_data);
				case GameInfoModel.GAME_OVER:					
									
					break;		
				//case ChipsView.MOVE_CHIPS_COMPLETE:	
					//showMadeHand();
					break;
				case GameInfoModel.LOSE_PLAYER:
					_clearTableCards([r_data["sit"]]);
					break;
			}
		}		
		
		/**
		 * 發公共牌
		 * @param	r_data Object {[牌型,花色],[牌型,花色],....}
		 */
		private function _setCommunityCards(r_data : Object) : void
		{
			if (_poker_class) {
				var _val : Array;
				for each(_val in r_data) {						
					this._showPoker(_val);													
				}				
			}
		}		
		
		/**
		 * 显示扑克
		 * @param	r_data Array  [牌型,花色] 
		 * @param	r_sit int  玩家ID
		 */
		private function _showPoker(r_data : Array,r_sit : int = 0) : void
		{		
			var _arr : Array = [];
			var _index : String;
			var _position : Object;		
			var _mc = new _poker_class;
			_mc.initialize(r_data);
			if (r_sit == 0) { //公共牌
				_public_poker++
				if (_public_poker > 5) {
					_public_poker = 1;
				}
				_mc.x = 270+(_public_poker-1)*45;
				_mc.y = 155;	
				_container.addChild(_mc);
			}else if (r_sit > 0 ) {//私有牌	
				_position = this._poker_pos[r_sit];				
				_mc.x = _position["x"];
				_mc.y = _position["y"];	
				var _bool : Boolean = this._two_poker[r_sit] ;
				if (_bool) {				
					_mc.x += 46;													
				}
				this._two_poker[r_sit] = !_bool;
				_container.addChild(_mc);
			}	
			var _first_index : String = r_data[0].toLocaleUpperCase();
			_index = _first_index + r_data[1];
			this._all_poker[_index] = _mc ;											
		}
		
		/**
		 * 设置玩家桌面撲克牌
		 * @param	r_sitList Array [座位号,....]
		 */		
		private function _setTableCards(r_sitList : Array ) : void
		{
			var _list : Object = r_sitList;			
			var _sit : int;
			var _deal_num : int = 2; //發牌次數
			var _i : int = 0;
			while(_deal_num >0){
				for each(_sit in _list) {					
					var _mc = new _card_class;
					_mc.alpha = 0;
					_mc.x = 376;
					_mc.y = 141;
					_mc.rotation = this._cards_pos[_sit].r;
					if(_deal_num == 1){
						_mc.rotation -= 15;
					}
					this._container.addChild(_mc);	
					var _cards  = this._table_poker[_sit];
					this._table_poker[_sit] = _cards != null ? _cards.concat(_mc): [_mc];													
					var _option : Object = [];
					_option["x"] = this._cards_pos[_sit].x;
					_option["y"] = this._cards_pos[_sit].y;
					_option["time"] = 0.7;
					_option["delay"] = _i*0.4;					
					_option["onStart"] = _tweenStart;
					_option["onStartParams"] = [_mc];
					_option["onComplete"] = _tweenComplete;
					_option["transition"] = "easeOutQuart";				
					_option["onCompleteParams"] = [_mc, (2-_deal_num), _sit];				
					Tweener.addTween(_mc, _option );	
					_i++;
				}
				_deal_num--;
			}
		}
		
		private function _tweenStart(r_obj : Object) : void
		{
			r_obj.alpha = 1;
		}
		
		private function _tweenComplete(r_obj : Object,r_deal_num : int, r_sit : int) : void {			
			Tweener.removeTweens(r_obj, "x", "y");	
			var _poker : Array = this._user_poker[r_sit];
			if(_poker is Array){					
				var _show_poker : Array = _poker[r_deal_num];					
				_showPoker(_show_poker, r_sit);				
			}
		}
		
		/**
		 * 显示赢家扑克
		 * 
		 */
		private function _showWinUserPoker(r_data) : void
		{
			var _poker : Array;
			var _data : Object = r_data;
			var _sit : int;
			var _uid : int;
			for (var k in _data) {
				_poker = _data[k]["poker"];
				_uid = _data["uid"];
				_sit = k;
				if(!_user_poker[_sit]){
					_user_poker[_sit] = _poker;
					_showPoker(_poker[0],_sit);
					_showPoker(_poker[1], _sit);					
				}
			}	
		}
		
		/**
		 * 清除桌面上反面的撲克牌
		 * @param	r_data array [sit,...]
		 */
		private function _clearTableCards(r_data : Array) : void
		{
			var _val : String;
			var _arr : Array;
			for each(_val in r_data) {
				_arr = this._table_poker[_val];
				if (_arr!= null) {
					for each(var _obj in _arr) {
						if (_obj != null && _obj.parent) {
							_obj.parent.removeChild(_obj);
						}
					}
				}
				delete this._table_poker[_val];											
			}
		}
		
		/**
		 * 清除某個玩家正面的扑克牌
		 * @param	r_data [sit,...]
		 */
		private function _clearCards(r_data : Array) : void
		{			
			var _val : String;
			var _arr : Array;
			var _poker : Array;
			var _obj : Object;
			for each(_val in r_data) {				
				_poker = _user_poker[_val];
				for each(_arr in _poker) {
					_obj = _all_poker[_arr[0] + _arr[1]];
					if (_obj != null && _obj.parent) {
						_obj.parent.removeChild(_obj);						
					}
					delete _all_poker[_arr[0] + _arr[1]];
				}
				delete _user_poker[_val];								
			}
			this._table_poker = { };	
		}
						
		/**
		 * 清除撲克牌
		 */
		private function _clearPoker( ) : void
		{			
			var _list : Object = this._all_poker;
			var _obj : Object;
			for each(_obj in _list) {
				if (_obj != null && _obj.parent != null) {
					_obj.parent.removeChild(_obj);
				}
			}
			this._all_poker = { };
			this._user_poker = { };			
			this._public_poker = 0
		}
		
		/**
		 * 突出显示赢家成手牌
		 * @param	r_arr Array [sit,sit,...],赢家座位号
		 */
		public function showMadeHand(r_arr : Array ) : void
		{			
			var _obj : Object;
			var _poker : Array = [];
			var _val : Array;			
			var _sit : int;
			var _result : Object;			
			for each(_sit in r_arr) {
				_poker = _poker.concat(this._game_result[_sit]["winPoker"]); 				
			}		
			for each(_obj in this._all_poker) {
				if (_obj) {
					_obj.setAlpha(0.6);
				}
			}
			for each(_val in _poker) {
				_obj = this._all_poker[String(_val[0]).toLocaleUpperCase() + _val[1]];
				if (_obj != null) {
					_obj.setAlpha(1);										
				}
			}			
		}
		
		override public function destroy():void 
		{
			Tweener.resumeAllTweens();
			Tweener.removeAllTweens();
			RemoveAllChildren.remove(_container as DisplayObject);
			this._container.parent.removeChild(this._container);
		}
				
	
	}	
	
}
